package com.yanqu.road.server.manger.activity.strongholdwar;

import com.alibaba.fastjson.JSON;
import com.yanqu.data.analytics.constant.EventDataConstants;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.stronghold.*;
import com.yanqu.road.entity.activity.stronghold.config.StrongholdTradeConfig;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.enums.ePatronsSkillType;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.player.UserPatronsSkill;
import com.yanqu.road.entity.rank.UnionActivityRank;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.bussiness.player.UserPatronsBussiness;
import com.yanqu.road.logic.bussiness.player.activity.StrongholdWarDonateBusiness;
import com.yanqu.road.logic.bussiness.player.activity.UserStrongholdBusiness;
import com.yanqu.road.logic.helper.UnionHelper;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.server.gameplayer.module.cricket.CricketMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.curio.CurioMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.pb.activity.StrongholdWarCrossProto;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.pb.rank.UnionActivityRankProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.strongholdwar.StrongholdModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.server.manger.config.VehicleMgr;
import com.yanqu.road.server.manger.cross.CrossDataMgr;
import com.yanqu.road.server.manger.manor.FishPondMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;


public class StrongholdWarMgr extends TempMgr {

    private static Map<Integer, Map<String, StrongholdUnionBase>> activityUnionBaseMap;

    /**
     * 银两捐献
     */
    private static Map<Integer, Map<String, Map<Long, StrongholdSilverDonate>>> silverDonateMap;
    /**
     * 道具捐献
     */
    private static Map<Integer, Map<String, Map<Long, Map<Integer, StrongholdItemDonate>>>> itemDonateMap;

    /**
     * 派遣的防守门客信息，每一次结束派遣后，同步到跨服
     */
    private static Map<Integer, Map<String, Map<Long, List<StrongholdEliminateDefendPatrons>>>> defendPatronsMap;

    /**
     * key1:activityId key2:groupId, key3:round
     */
    private static Map<Integer, Map<Integer, StrongholdUploadMark>> uploadMarkMap;

    private static Map<Integer, Map<Long, StrongholdTradeData>> tradeMap;

    /**
     * 派遣防守门客
     *
     * @param unionUid
     * @param userId
     * @param patronsList
     */
    public static List<StrongholdEliminateDefendPatrons> dispatchDefendPatrons(int activityId, String unionUid, long userId, List<UserPatrons> patronsList, Map<Integer, UserPatrons> userPatronsMap) {

        Map<Long, List<StrongholdEliminateDefendPatrons>> patronsMap = getUnionDefendPatronsMap(activityId, unionUid);
        List<StrongholdEliminateDefendPatrons> defendPatronsList = patronsMap.get(userId);
        if (defendPatronsList == null) {
            defendPatronsList = new ArrayList<>();
            patronsMap.put(userId, defendPatronsList);
        }
        for (int i = 0; i < 2; i++) {
            UserPatrons patrons = null;
            if (patronsList.size() > i) {
                patrons = patronsList.get(i);
            }
            StrongholdEliminateDefendPatrons eliminatePatrons;
            if (defendPatronsList.size() <= i) {
                //如果派遣的防守门客小于当前要派遣的数量
                StrongholdEliminateDefendPatrons defendPatrons = defendPatronsList.get(i);
                eliminatePatrons = createEliminatePatrons(activityId, userId, patrons, unionUid, i, userPatronsMap,defendPatrons, 0);
                defendPatronsList.add(eliminatePatrons);
            }
            eliminatePatrons = defendPatronsList.get(i);
            eliminatePatrons.setUserBaseInfo(UserMgr.getUserBaseInfo(userId, GameServer.getInstance().getServerId()));
            if (patrons != null) {
                BigInteger totalAbility = calcStrongholdPatronsTotalAbility(patrons, userPatronsMap, 0);
                eliminatePatrons.setEarnSpeed(totalAbility);
                BigInteger blood = StrongholdActivityMgr.calcBlood(totalAbility);
                eliminatePatrons.setTotalBlood(blood);
                eliminatePatrons.setPatrons(patrons.cloneUserPatrons());
            } else {
                eliminatePatrons.setEarnSpeed(BigInteger.ZERO);
                eliminatePatrons.setTotalBlood(BigInteger.ZERO);
                eliminatePatrons.setPatrons(null);
            }
        }
        return defendPatronsList;
    }

    /**
     * 获取联盟所有防守门客
     *
     * @param activityId
     * @param unionUid
     * @return
     */
    private static Map<Long, List<StrongholdEliminateDefendPatrons>> getUnionDefendPatronsMap(int activityId, String unionUid) {
        synchronized (defendPatronsMap) {
            Map<String, Map<Long, List<StrongholdEliminateDefendPatrons>>> unionMap = defendPatronsMap.get(activityId);
            if (unionMap == null) {
                unionMap = new ConcurrentHashMap<>();
                defendPatronsMap.put(activityId, unionMap);
            }
            Map<Long, List<StrongholdEliminateDefendPatrons>> patronsMap = unionMap.get(unionUid);
            if (patronsMap == null) {
                patronsMap = new ConcurrentHashMap<>();
                unionMap.put(unionUid, patronsMap);
            }
            return patronsMap;
        }
    }

    /**
     * 获取联盟据点（捐献数据）
     *
     * @param activityId
     * @param unionId
     * @return
     */
    private static StrongholdUnionBase getUnionBase(int activityId, String unionId) {
        synchronized (activityUnionBaseMap) {
            Map<String, StrongholdUnionBase> unionBaseMap = activityUnionBaseMap.get(activityId);
            if (unionBaseMap == null) {
                unionBaseMap = new ConcurrentHashMap<>(4);
                activityUnionBaseMap.put(activityId, unionBaseMap);
            }
            StrongholdUnionBase unionBase = unionBaseMap.get(unionId);
            if (unionBase == null) {
                unionBase = initNewUnionBase(activityId, unionId);
                unionBaseMap.put(unionId, unionBase);
            }
            return unionBase;
        }
    }

    /**
     * 初始化联盟数据
     *
     * @param activityId
     * @param unionId
     * @return
     */
    private static StrongholdUnionBase initNewUnionBase(int activityId, String unionId) {
        StrongholdUnionBase unionBase;
        unionBase = new StrongholdUnionBase();
        unionBase.setActivityId(activityId);
        unionBase.setUnionUid(unionId);
        unionBase.setLv(0);
        List<Integer> processList = new ArrayList<>();
        processList.add(0);
        processList.add(0);
        processList.add(0);
        unionBase.setCurProcess(processList);
        unionBase.setInsertOption();
        return unionBase;
    }

    /**
     * 捐献道具
     */
    public static void donateItem(int activityId, String unionId, int type, int addValue) {
        StrongholdUnionBase unionBase = getUnionBase(activityId, unionId);
        synchronized (unionBase) {
            unionBase.addProcess(type, addValue);
            int nextLv = StrongholdActivityMgr.getCurBaseLv(unionBase.getLv(), unionBase.getCurProcess());
            unionBase.setLv(nextLv);
        }
    }


    /**
     * 找出有资格参加淘汰赛的联盟
     *
     * @param activityId
     * @return
     */
    private static Map<Integer, Set<String>> findQualifyList(int activityId) {
        Map<Integer, UnionActivityGroup> groupMap = GameUnionActivityGroupMgr.getGroupMap(activityId);
        if (null == groupMap) {
            return new ConcurrentHashMap<>();
        }
        boolean needSync = false;
        Map<Integer, Set<String>> qualifyListMap = new ConcurrentHashMap<>();
        for (UnionActivityGroup activityGroup : groupMap.values()) {
            Set<String> qualifyList = getQualifyUnionList(activityId, activityGroup.getGroupId(), false);
            Map<Integer, StrongholdUploadMark> markMap = uploadMarkMap.get(activityId);
            if (markMap == null) {
                markMap = new ConcurrentHashMap<>();
                uploadMarkMap.put(activityId, markMap);
            }
            int round = StrongholdActivityMgr.getCurEmilinateRound();
            StrongholdUploadMark mark = markMap.get(round);

            if (mark == null) {
                mark = new StrongholdUploadMark();
                mark.setActivityId(activityId);
                mark.setRound(round);
                mark.setInsertOption();
                needSync = true;
                markMap.put(round, mark);
            }
            for (String unionId : qualifyList) {
                mark.addQualifyUnion(unionId);
            }

            qualifyListMap.put(activityGroup.getGroupId(), qualifyList);
        }

        if (needSync) {
            //重新通知玩家是否有资格
            for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
                player.getModule(StrongholdModule.class).syncUserData();
            }
        }
        return qualifyListMap;
    }

    public static Set<String> getQualifyUnionList(int activityId, int groupId, boolean allServer) {
        Set<String> qualifyList = new HashSet<>();
        if (StrongholdActivityMgr.isLookWildRank()) {
            //野外据点前几名
            int rankNum = StrongholdActivityMgr.getQualifyNum();
            UnionActivityRankProto.UnionActivityRankListMsg rankList = CrossDataMgr.getCrossActivityUnionConditionTypeRankList(activityId, groupId, eGamePlayerEventType.StrongholdWildUnionScoreRank.getValue());
            if (rankList != null) {
                qualifyList.addAll(findServerQualifyUnion(rankNum, rankList, allServer));
            }
        } else {
            // 淘汰赛前几名
            int rankNum = StrongholdActivityMgr.getQualifyNum();
            UnionActivityRankProto.UnionActivityRankListMsg rankList = CrossDataMgr.getCrossActivityUnionConditionTypeRankList(activityId, groupId, eGamePlayerEventType.StrongholdEliminateUnionRank.getValue());
            if (rankList != null) {
                qualifyList.addAll(findServerQualifyUnion(rankNum, rankList, allServer));
            }
        }
        return qualifyList;
    }

    /**
     * 捐献银两
     */
    @Deprecated
    public static void donateSilver(int activityId, String unionId, long userId, BigInteger num) {
        synchronized (silverDonateMap) {
            Map<Long, StrongholdSilverDonate> donateMap = getUnionSilverDonateMap(activityId, unionId);
            StrongholdSilverDonate silverDonate = donateMap.get(userId);
            if (silverDonate == null) {
                silverDonate = new StrongholdSilverDonate();
                silverDonate.setUserId(userId);
                silverDonate.setNum(BigInteger.ZERO);
                silverDonate.setUnionUid(unionId);
                silverDonate.setActivityId(activityId);
                silverDonate.setInsertOption();
                donateMap.put(userId, silverDonate);
            }
            silverDonate.setUserBaseInfo(UserMgr.getUserBaseInfo(userId, GameServer.getInstance().getServerId()));
            silverDonate.setNum(silverDonate.getNum().add(num));
        }
        Map<Long, StrongholdSilverDonate> donateMap = silverDonateMap.get(activityId).get(unionId);
        BigInteger total = BigInteger.ZERO;
        for (StrongholdSilverDonate donate : donateMap.values()) {
            total = total.add(donate.getNum());
        }
        //同步到跨服进行排名
        UnionActivityRank unionRank = new UnionActivityRank();
        unionRank.setActivityId(activityId);
        unionRank.setUnionUid(unionId);
        unionRank.setValue(total);
        unionRank.setLastUpdateTime(System.currentTimeMillis());
        UnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionId);
        unionRank.setUnionBaseInfo(UnionHelper.parseUnionBaseInfo(unionInfo,UserMgr.getUserInfo(unionInfo.getMasterUserId()), GameServer.getInstance().getServerId()));
        CrossDataMgr.strongholdUnionRankChange(activityId, eGamePlayerEventType.StrongholdDonateSilverRank.getValue(), unionRank);
    }

    /**
     * 获取联盟银两捐献内容
     *
     * @param activityId
     * @param unionId
     * @return
     */
    private static Map<Long, StrongholdSilverDonate> getUnionSilverDonateMap(int activityId, String unionId) {
        synchronized (silverDonateMap) {
            Map<String, Map<Long, StrongholdSilverDonate>> unionMap = silverDonateMap.get(activityId);
            if (unionMap == null) {
                unionMap = new ConcurrentHashMap<>(4);
                silverDonateMap.put(activityId, unionMap);
            }
            Map<Long, StrongholdSilverDonate> donateMap = unionMap.get(unionId);
            if (donateMap == null) {
                donateMap = new ConcurrentHashMap<>(4);
                unionMap.put(unionId, donateMap);
            }
            return donateMap;
        }
    }

    /**
     * 获取联盟捐献进度
     *
     * @param activityId
     * @param unionUid
     * @return
     */
    public static List<Integer> getDonateItemProcess(int activityId, String unionUid) {
        StrongholdUnionBase unionBase = getUnionBase(activityId, unionUid);
        return unionBase.getCurProcess();
    }

    /**
     * 获取道具捐献详情
     *
     * @param activityId
     * @param unionUid
     * @return
     */
    public static List<StrongholdSilverDonate> getSilverDonateDetail(int activityId, String unionUid) {
        Map<Long, StrongholdSilverDonate> donateMap = getUnionSilverDonateMap(activityId, unionUid);
        return new ArrayList<>(donateMap.values());
    }

    /**
     * 上传淘汰赛联盟
     */
    public static void uploadCrossEliminateUnion() {
        ActivityInfo activityInfo = StrongholdActivityMgr.getActivityInfo();
        if (activityInfo != null) {
            if (StrongholdActivityMgr.isUploadWildRankTime() || StrongholdActivityMgr.isEliminateAutoDispatchPatronsTime()) {
                int activityId = activityInfo.getActivityId();
                Map<Integer, Set<String>> qualifyListMap = findQualifyList(activityId);
                for (Map.Entry<Integer, Set<String>> groupAndListEntry : qualifyListMap.entrySet()) {
                    int groupId = groupAndListEntry.getKey();
                    Set<String> qualifyList = groupAndListEntry.getValue();
                    sendQualifyUnionListToCross(activityId, groupId, qualifyList);
                }
            }
        }
    }

    /**
     * 更新联盟数据
     * @param unionUid
     * @param unionInfo
     */
    public static void unionChange(String unionUid, CrossUnionInfo unionInfo) {
        try {
            ActivityInfo activityInfo = StrongholdActivityMgr.getActivityInfo();
            if (ActivityMgr.activityInShowTime(activityInfo)) {
                // 判断有无参赛资格
                if (!StrongholdWarMgr.hasQualification(activityInfo.getActivityId(), unionUid)) {
                    return;
                }
                StrongholdWarProto.CrossUpdateUnionBaseMsgReq.Builder builder = StrongholdWarProto.CrossUpdateUnionBaseMsgReq.newBuilder();
                builder.setActivityId(activityInfo.getActivityId());
                builder.setUnionUid(unionUid);
                builder.setUnionInfo(UnionBasePb.parseUnionBaseTempMsg(CrossUnionMgr.parseUnionBaseInfo(unionInfo)));
                int groupId = GameUnionActivityGroupMgr.getGroupId(activityInfo.getActivityId(), unionUid);
                GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C_CROSS_STRONGHOLD_UPDATE_UNION_BASE, builder, 0, activityInfo.getActivityId(), groupId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 发送有资格的联盟数据去跨服
     *
     * @param activityId
     * @param qualifyList
     */
    private static void sendQualifyUnionListToCross(int activityId, int groupId, Collection<String> qualifyList) {
        Map<String, StrongholdUnionBase> unionBaseMap = activityUnionBaseMap.get(activityId);
        if (unionBaseMap == null) {
            unionBaseMap = new ConcurrentHashMap<>();
            activityUnionBaseMap.put(activityId, unionBaseMap);
        }
        List<CrossStrongholdEliminateUnion> unionList = new ArrayList<>();
        for (String unionUid : qualifyList) {
            CrossStrongholdEliminateUnion eliminateUnion = createCrossEliminateUnion(activityId, unionBaseMap, unionUid);
            unionList.add(eliminateUnion);
        }
        if (unionList.size() <= 0) {
            return;
        }
        // 发送到跨服
        StrongholdWarProto.UploadEliminateUnionDataToCrossMsgReqMsg.Builder req = StrongholdWarProto.UploadEliminateUnionDataToCrossMsgReqMsg.newBuilder();
        req.setActivityId(activityId);
        for (CrossStrongholdEliminateUnion eliminateUnion : unionList) {
            StrongholdWarProto.UploadEliminateUnionMsg.Builder unionMsg = StrongholdWarProto.UploadEliminateUnionMsg.newBuilder();
            String unionUid = eliminateUnion.getUnionUid();
            StrongholdUnionBase unionBase = getUnionBase(activityId, eliminateUnion.getUnionUid());
            StrongholdWarProto.UnionBaseDataMsg.Builder msg = StrongholdWarProto.UnionBaseDataMsg.newBuilder();
            msg.setUnionUid(unionUid);
            msg.setLv(unionBase.getLv());
            msg.setUnionInfo(UnionBasePb.parseUnionBaseTempMsg(eliminateUnion.getUnionBaseInfo()));
            unionMsg.setUnion(msg);
            for (CrossStrongholdEliminateDefendPatrons defendPatrons : eliminateUnion.getDefendPatronsList()) {
                StrongholdWarProto.EliminatePatronsDataMsg.Builder patronsMsg = StrongholdWarProto.EliminatePatronsDataMsg.newBuilder();
                patronsMsg.setPlayerData(PlayerBasePb.parsePlayerBaseTempMsg(defendPatrons.getUserBaseInfo()));
                patronsMsg.setPatrons(JSON.toJSONString(defendPatrons.getPatrons()));
                patronsMsg.setTottalBlood(defendPatrons.getTotalBlood().toString());
                patronsMsg.setIndex(defendPatrons.getPatronsIndex());
                unionMsg.addPatrons(patronsMsg);
            }
            req.addUnion(unionMsg);
        }

        GamePlayerMgr.sendUnionActivityGroupPacket(Protocol.C_CROSS_STRONGHOLD_UPLOAD_UNION_PATRONS, req, 0, activityId, groupId);
    }

    /**
     * 创建淘汰赛联盟数据
     *
     * @param activityId
     * @param unionBaseMap
     * @param unionUid
     * @return
     */
    private static CrossStrongholdEliminateUnion createCrossEliminateUnion(int activityId, Map<String, StrongholdUnionBase> unionBaseMap, String unionUid) {
        CrossStrongholdEliminateUnion eliminateUnion = new CrossStrongholdEliminateUnion();
        StrongholdUnionBase unionBase = unionBaseMap.get(unionUid);
        if (unionBase == null) {
            unionBase = initNewUnionBase(activityId, unionUid);
            unionBaseMap.put(unionUid, unionBase);
        }

        eliminateUnion.setActivtyId(activityId);
        eliminateUnion.setServerId(GameServer.getInstance().getServerId());
        eliminateUnion.setUnionUid(unionUid);
        CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionUid);
        CrossUnionMember master = CrossUnionMgr.getUnionMember(unionInfo.getMasterUserId());
        UserBaseInfo userBaseInfo = null;
        if (master != null) {
            userBaseInfo   = master.getUserBaseInfo();
        }
        eliminateUnion.setUnionBaseInfo(UnionHelper.parseUnionBaseInfoByUserBaseInfo(unionInfo,userBaseInfo, unionInfo.getServerId()));
        eliminateUnion.getUnionBaseInfo().setServerId(unionInfo.getServerId());
        List<StrongholdEliminateDefendPatrons> list = initDefendPatrons(activityId, unionUid);
        eliminateUnion.addDefendPatronsList(list);
        return eliminateUnion;
    }

    /**
     * 初始化淘汰赛防守门客信息
     *
     * @param activityId
     * @param unionUid
     * @return
     */
    private static List<StrongholdEliminateDefendPatrons> initDefendPatrons(int activityId, String unionUid) {
        List<StrongholdEliminateDefendPatrons> list = new ArrayList<>();
        getLogger().info("{} 开始初始化防守门客", unionUid);
        Map<Long, List<StrongholdEliminateDefendPatrons>> patronsMap = getUnionDefendPatronsMap(activityId, unionUid);
        Map<Long, CrossUnionMember> memberMap = CrossUnionMgr.getUnionMemberMap(unionUid);
        for (Long userId : memberMap.keySet()) {
            //  boolean hasDispatched = isHasDispatched(patronsMap, userId);
            //  if (!hasDispatched) {
            //未派遣 自动帮玩家派遣
            if (StrongholdActivityMgr.isEliminateAutoDispatchPatronsTime() ) {
                if (StrongholdActivityMgr.isNeedResetEliminateAutoDispatchPatronsTime()) {
                    autoDispatchPatrons(activityId,  userId, unionUid, 0);
                }else {
                    List<StrongholdEliminateDefendPatrons> patronsList = patronsMap.get(userId);
                    if (patronsList != null && patronsList.size() >= 2) {
                        // 已经自动派遣 且不是最后一轮
                        continue;
                    }
                    autoDispatchPatrons(activityId,  userId, unionUid, 0);
                }
            }
            // }
        }
        int hpAdd = 0;//getHpAdd(activityId, unionUid);

        BigInteger totalBlood = BigInteger.ZERO;
        int patronsNum = 0;
        for (List<StrongholdEliminateDefendPatrons> patronsList : patronsMap.values()) {
            for (StrongholdEliminateDefendPatrons patrons : patronsList) {
                if (patrons.getPatrons() != null) {
                    totalBlood = totalBlood.add(patrons.getTotalBlood().add(patrons.getTotalBlood().multiply(BigInteger.valueOf(hpAdd)).divide(new BigInteger("1000"))));
                    patronsNum++;
                }
            }
        }
        for (List<StrongholdEliminateDefendPatrons> patronsList : patronsMap.values()) {
            for (StrongholdEliminateDefendPatrons patrons : patronsList) {
                if (patrons.getPatrons() != null) {
                    StrongholdEliminateDefendPatrons clonePatrons = patrons.clonePatrons();
                    clonePatrons.setTotalBlood(totalBlood.divide(BigInteger.valueOf(patronsNum)));
                    list.add(clonePatrons);
                }
            }
        }
        if (StrongholdActivityMgr.isUploadEliminatePatronsTime()) {
            for (StrongholdEliminateDefendPatrons patrons : list) {
                long userId = patrons.getUserId();
                int patronsId = patrons.getPatrons().getPatronsId();
                VehicleMgr.bindVehicleToPatrons(activityId, userId, patronsId);
                FishPondMgr.bindFishToPatrons(activityId, userId, patronsId);
                CricketMgr.bindCricketToPatrons(activityId, userId, patronsId);
            }
        }
        return list;
    }

    private static boolean isHasDispatched(Map<Long, List<StrongholdEliminateDefendPatrons>> patronsMap, Long userId) {
        boolean hasDispatched = false;
        List<StrongholdEliminateDefendPatrons> defendPatronsList = patronsMap.get(userId);
        if (defendPatronsList != null) {
            for (StrongholdEliminateDefendPatrons patrons : defendPatronsList) {
                if (patrons.getPatrons() != null) {
                    hasDispatched = true;
                    break;
                }
            }
        }
        return hasDispatched;
    }

    /**
     * 获取淘汰赛血量加成
     *
     * @param activityId
     * @param unionUid
     * @return
     */
    public static int getHpAdd(int activityId, String unionUid) {
        StrongholdUnionBase unionBase = getUnionBase(activityId, unionUid);
        int lv = unionBase.getLv();
        //千分比
        int unionBaseAdd = StrongholdActivityMgr.getUnionBaseAdd(lv);
        int hpAddByRank = getHpAddBySilverRank(activityId, unionUid);
        return unionBaseAdd + hpAddByRank;
    }

    /**
     * 获取银两捐献排行加成
     *
     * @param activityId
     * @param unionUid
     * @return
     */
    private static int getHpAddBySilverRank(int activityId, String unionUid) {
        int rank = getSilverRank(activityId, unionUid);
        //千分比
        return StrongholdActivityMgr.getHpAddByRank(rank);
    }

    /**
     * 获取银两捐献排行
     * -1:未上榜
     * 0：已淘汰
     *
     * @param activityId
     * @param unionUid
     * @return
     */
    public static int getSilverRank(int activityId, String unionUid) {
        int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, unionUid);
        Collection<String> allQualifyUnion = getQualifyUnionList(activityId, groupId, true);
        if (!allQualifyUnion.contains(unionUid)) {
            return 0;
        }

        int myRank = -1;
        UnionActivityRankProto.UnionActivityRankListMsg rankList = CrossDataMgr.getCrossActivityUnionConditionTypeRankList(activityId, groupId, eGamePlayerEventType.StrongholdDonateSilverRank.getValue());
        if (rankList == null) {
            return myRank;
        }
        int rank = 0;
        for (UnionActivityRankProto.UnionActivityRankTempMsg msg : rankList.getRankListList()) {
            String uid = msg.getUnionUid();
            if (allQualifyUnion.contains(uid)) {
                rank++;
                if (msg.getUnionBaseData().getServerId() == GameServer.getInstance().getServerId() && msg.getUnionUid().equals(unionUid)) {
                    myRank = rank;
                    break;
                }
            }
        }
        return myRank;
    }

    /**
     * 自动派遣门客
     *
     * @param activityId
     * @param userId
     * @param unionId
     */
    public static void autoDispatchPatrons(int activityId,  Long userId, String unionId, long otherAddition) {
        getLogger().info("{} {} 开始自动派遣门客", unionId, userId);
        Map<Integer, UserPatrons> userPatronsMap = findPatronsMap(userId);
        Map<Long, List<StrongholdEliminateDefendPatrons>> patronsMap = getUnionDefendPatronsMap(activityId, unionId);
        if (userPatronsMap != null) {
            List<StrongholdEliminateDefendPatrons> defendPatronsList = patronsMap.get(userId);
            if (defendPatronsList == null) {
                defendPatronsList = new ArrayList<>();
                patronsMap.put(userId, defendPatronsList);
            }
            int minNum = Math.min(userPatronsMap.size(), 2);
            if (minNum <= 0) {
                return;
            }
            for (int rank = 1; rank <= minNum; rank++) {
                UserPatrons patrons = PatronsMgr.getUserPatronsByEarnSpeedRank(rank, new ArrayList<>(userPatronsMap.values()));
                int index = rank - 1;
                if (patrons != null) {
                    getLogger().info("{} {} 自动派遣门客 {}", unionId, userId, patrons.getPatronsId());
                    if (defendPatronsList.size() < rank) {
                        StrongholdEliminateDefendPatrons defendPatrons = createEliminatePatrons(activityId, userId, patrons, unionId, rank - 1, userPatronsMap,null, otherAddition);
                        defendPatrons.setInsertOption();
                        defendPatronsList.add(defendPatrons);
                    }else {
                        StrongholdEliminateDefendPatrons defendPatron = defendPatronsList.get(index);
                        createEliminatePatrons(activityId, userId, patrons, unionId, rank - 1, userPatronsMap,defendPatron, otherAddition);
                    }

                    GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
                    if (player != null) {
                        //顺便通知下玩家自动派遣门客了
                        player.getModule(StrongholdModule.class).syncUserData();
                    }
                }
            }
        }
    }

    /**
     * 创建防守门客信息
     *
     * @param activityId
     * @param userId
     * @param patrons
     * @param unionId
     * @param index
     * @return
     */
    private static StrongholdEliminateDefendPatrons createEliminatePatrons(int activityId, Long userId, UserPatrons patrons, String unionId, int index, Map<Integer, UserPatrons> userPatronsMap,StrongholdEliminateDefendPatrons defendPatrons, long otherAddition) {
        if (defendPatrons == null) {
            defendPatrons = new StrongholdEliminateDefendPatrons();
        }
        defendPatrons.setActivityId(activityId);
        defendPatrons.setUserId(userId);
        defendPatrons.setUnionUid(unionId);
        defendPatrons.setUserBaseInfo(UserMgr.getUserBaseInfo(userId, GameServer.getInstance().getServerId()));
        defendPatrons.setPatronsSite(index);
        if (patrons != null) {
            BigInteger totalAbility = calcStrongholdPatronsTotalAbility(patrons, userPatronsMap, otherAddition);
            BigInteger blood = StrongholdActivityMgr.calcBlood(totalAbility);
            defendPatrons.setTotalBlood(blood);
            defendPatrons.setEarnSpeed(totalAbility);
            defendPatrons.setPatrons(patrons.cloneUserPatrons());
            defendPatrons.getPatrons().setTotalBlood(totalAbility);
        }
        return defendPatrons;
    }

    public static BigInteger calcStrongholdPatronsTotalAbility(UserPatrons patrons, Map<Integer, UserPatrons> userPatronsMap, long otherAddition) {
        int addition = getStrongholdAblilityAddition(patrons, userPatronsMap);
        addition += otherAddition;
        return patrons.getAbility().add(new BigDecimal(patrons.getAbility()).multiply(new BigDecimal(addition)).divide(new BigDecimal("1000"), 0, BigDecimal.ROUND_UP).toBigInteger());
    }

    /**
     * 获取据点战赚速加成
     * @param patrons 门客
     * @param userPatronsMap
     * @return
     */
    public static int getStrongholdAblilityAddition(UserPatrons patrons, Map<Integer, UserPatrons> userPatronsMap) {
        int addition = 0;
        for (UserPatrons userPatrons : userPatronsMap.values()) {
            for(UserPatronsSkill patronsSkill : userPatrons.getSkillList()){
                if(patronsSkill.getType() != ePatronsSkillType.Talent.getValue()){
                    continue;
                }
                SkillInfo skillInfo = SkillMgr.getSkillInfo(patronsSkill.getSkillId());
                if(skillInfo.getType() == eSkillType.StrongholdAdditionSkill.getValue()){ //这个技能从对自身加成 改成 对所有门客加成
                    addition += SkillMgr.getSkillAddition(skillInfo, patronsSkill.getSkillLv());
                }
            }
        }
        if (patrons != null) {
            for(UserPatronsSkill patronsSkill : patrons.getSkillList()){
                if(patronsSkill.getType() != ePatronsSkillType.Talent.getValue()){
                    continue;
                }
                SkillInfo skillInfo = SkillMgr.getSkillInfo(patronsSkill.getSkillId());
                if (StrongholdActivityMgr.isAfterWildTime()) {
                    if(skillInfo.getType() == eSkillType.StrongholdEliminateAdditionSkill.getValue()){
                        addition += SkillMgr.getSkillAddition(skillInfo, patronsSkill.getSkillLv());
                    }
                }
            }
        }
        return addition;
    }

    /**
     * 找出自动派遣的门客
     *
     * @param userId
     * @return
     */
    private static Map<Integer, UserPatrons> findPatronsMap(long userId) {
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);

        if (player != null) {
            return player.getModule(PatronsModule.class).getUserPatronsMap();
        }
        Map<Integer, UserPatrons> patronsMap = UserPatronsBussiness.getUserPatronsAndSkillMap(userId);
        if (patronsMap == null || patronsMap.size() <= 0) {
            return null;
        }
        return patronsMap;
    }


    /**
     * 找出本服有资格的联盟
     *
     * @param rankNum
     * @param rankList
     * @param allServer
     * @return
     */
    private static List<String> findServerQualifyUnion(int rankNum, UnionActivityRankProto.UnionActivityRankListMsg rankList, boolean allServer) {
        int rank = 0;
        List<String> unionUidList = new ArrayList<>();
        for (UnionActivityRankProto.UnionActivityRankTempMsg msg : rankList.getRankListList()) {
            rank++;
            String unionUid = msg.getUnionUid();
            if (allServer || CrossUnionMgr.getUnionInfo(unionUid) != null) {
                unionUidList.add(unionUid);
            }
            if (rank >= rankNum) {
                break;
            }
        }
        return unionUidList;
    }


    /**
     * 获取淘汰赛联盟防守门客信息
     *
     * @param activityId
     * @param unionId
     * @return
     */
    public static List<StrongholdEliminateDefendPatrons> getEliminateDefendList(int activityId, String unionId) {
        List<StrongholdEliminateDefendPatrons> patronsList = new ArrayList<>();
        Map<String, Map<Long, List<StrongholdEliminateDefendPatrons>>> unionMap = defendPatronsMap.get(activityId);
        if (unionMap == null) {
            return patronsList;
        }
        Map<Long, List<StrongholdEliminateDefendPatrons>> patronsMap = unionMap.get(unionId);
        if (patronsMap == null) {
            return patronsList;
        }
        for (List<StrongholdEliminateDefendPatrons> defendPatronsList : patronsMap.values()) {
            for (StrongholdEliminateDefendPatrons patrons : defendPatronsList) {
                if (patrons.getPatrons() != null) {
                    patronsList.add(patrons);
                }
            }
        }
        return patronsList;
    }

    /**
     * 判断联盟是否有资格
     *
     * @param activityId
     * @param unionUid
     * @return
     */
    public static boolean hasQualification(int activityId, String unionUid) {
        Map<Integer, StrongholdUploadMark> markMap = StrongholdWarMgr.uploadMarkMap.get(activityId);
        if (markMap == null) {
            return false;
        }
        int round = StrongholdActivityMgr.getCurEmilinateRound();
        StrongholdUploadMark mark = markMap.get(round);
        if (mark == null) {
            return false;
        }
        return mark.getQualifyUnionList().contains(unionUid);
    }

    /**
     * 获取玩家淘汰赛防守门客
     *
     * @param activityId
     * @param unionUid
     * @param userId
     * @return
     */
    public static List<Integer> getEliminateDefendPatrons(int activityId, String unionUid, long userId) {
        Map<String, Map<Long, List<StrongholdEliminateDefendPatrons>>> unionMap = defendPatronsMap.get(activityId);
        List<Integer> list = new ArrayList<>();
        if (unionMap == null) {
            return list;
        }
        Map<Long, List<StrongholdEliminateDefendPatrons>> patronsMap = unionMap.get(unionUid);
        if (patronsMap == null) {
            return list;
        }
        List<StrongholdEliminateDefendPatrons> patronsList = patronsMap.get(userId);
        if (patronsList == null) {
            return list;
        }
        for (StrongholdEliminateDefendPatrons patrons : patronsList) {
            if (patrons.getPatrons() != null) {
                list.add(patrons.getPatrons().getPatronsId());
            }
        }
        return list;
    }

    /**
     * 西域通商
     *
     * @param activityId
     * @param userId
     * @param unionUid
     * @param id
     * @param reward
     */
    public static void addTrade(int activityId, long userId, String unionUid, int id, String reward) {
        Map<Long, StrongholdTradeData> dataMap = tradeMap.get(activityId);
        if (dataMap == null) {
            synchronized (tradeMap){
                dataMap = tradeMap.get(activityId);
                if(dataMap == null){
                    dataMap = new ConcurrentHashMap<>();
                    tradeMap.put(activityId, dataMap);
                }
            }
        }
        StrongholdTradeData data = dataMap.get(userId);
        if (data != null) {
            return;
        }
        data = new StrongholdTradeData();
        data.setActivityId(activityId);
        data.setUserId(userId);
        data.setUnionId(unionUid);
        data.setAddressId(id);
        data.setTradeTime(System.currentTimeMillis() / 1000);
        data.setReward(reward);
        data.setInsertOption();
        dataMap.put(data.getUserId(), data);
        //上传到跨服
        StrongholdWarCrossProto.UploadTradeMsg.Builder msg = StrongholdWarCrossProto.UploadTradeMsg.newBuilder();
        msg.setActivityId(activityId);
        msg.setUnionId(unionUid);
        msg.setUserId(userId);
        msg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(userId, GameServer.getInstance().getServerId())));
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        if (player != null) {
            player.sendUnionActivityGroupPacket(CrossProtocol.C_CROSS_STRONGHOLD_TRADE, msg, msg.getActivityId());
        }
    }

    /**
     * 联盟西域通商成员
     *
     * @param activityId
     * @param userIdList
     * @return
     */
    public static Set<Long> getUnTradeUserList(int activityId, List<Long> userIdList) {
        Set<Long> userIdSet = new HashSet<>();
        Map<Long, StrongholdTradeData> dataMap = tradeMap.get(activityId);
        if (dataMap == null) {
            return userIdSet;
        }
        for (Long userId : userIdList) {
            if (!dataMap.containsKey(userId)) {
                userIdSet.add(userId);
            }
        }
        return userIdSet;
    }

    /**
     * 玩家西域通商地址
     *
     * @param activityId
     * @param userId
     * @return
     */
    public static int getMyTradeAddress(int activityId, long userId) {
        Map<Long, StrongholdTradeData> dataMap = tradeMap.get(activityId);
        if (dataMap == null) {
            return 0;
        }
        for (StrongholdTradeData data : dataMap.values()) {
            if (data.getUserId() == userId) {
                return data.getAddressId();
            }
        }
        return 0;
    }

    /**
     * 联盟道具捐献等级
     *
     * @param activityId
     * @param unionUid
     * @return
     */
    public static int getUnionBaseLv(int activityId, String unionUid) {
        StrongholdUnionBase unionBase = getUnionBase(activityId, unionUid);
        return unionBase.getLv();
    }

    public static void tradeTimeTask() {
        ActivityInfo activityInfo = StrongholdActivityMgr.getOpenActivityInfo();
        if (activityInfo != null) {
            if (StrongholdActivityMgr.activityInCanRewardPeriod()) {
                if (!noticeTradeRed) {
                    for (GamePlayer player : GamePlayerMgr.getCopyPlayerMap().values()) {
                        player.getModule(StrongholdModule.class).syncUserData();
                    }
                    noticeTradeRed = true;
                }
            }
        }
        checkTradeReturn();
    }

    public static boolean hasQualification(GamePlayer player) {
        ActivityInfo activityInfo = StrongholdActivityMgr.getOpenActivityInfo();
        if (activityInfo == null) {
            return false;
        }
        String unionUid = player.getUserInfo().getUnionUid();
        return hasQualification(activityInfo.getActivityId(), unionUid);
    }

    public static boolean canDismountVahicle(GamePlayer player) {
        ActivityInfo activityInfo = StrongholdActivityMgr.getOpenActivityInfo();
        if (activityInfo == null) {
            //没有活动可以
            return true;
        }
        if (StrongholdActivityMgr.isAfterWildTime()) {
            //野外结束
            //被淘汰 或者 淘汰赛结束
            return StrongholdActivityMgr.isGameFinish() || !StrongholdWarMgr.hasQualification(player);
        }
        return false;
    }

    public static void userDonate(int activityId, String unionId, long userId, int type, int addValue) {
        StrongholdItemDonate donate = getUserItemDonateData(activityId, unionId, userId, type);
        donate.setValue(donate.getValue() + addValue);
    }

    private static StrongholdItemDonate getUserItemDonateData(int activityId, String unionId, long userId, int type) {
        synchronized (itemDonateMap) {
            Map<String, Map<Long, Map<Integer, StrongholdItemDonate>>> unionMap = itemDonateMap.get(activityId);
            if (unionMap == null) {
                unionMap = new ConcurrentHashMap<>();
                itemDonateMap.put(activityId, unionMap);
            }
            Map<Long, Map<Integer, StrongholdItemDonate>> userMap = unionMap.get(unionId);
            if (userMap == null) {
                userMap = new ConcurrentHashMap<>();
                unionMap.put(unionId, userMap);
            }
            Map<Integer, StrongholdItemDonate> donateMap = userMap.get(userId);
            if (donateMap == null) {
                donateMap = new ConcurrentHashMap<>();
                userMap.put(userId, donateMap);
            }
            StrongholdItemDonate donate = donateMap.get(type);
            if (donate == null) {
                donate = new StrongholdItemDonate();
                donate.setActivityId(activityId);
                donate.setUnionUid(unionId);
                donate.setUserId(userId);
                donate.setType(type);
                donate.setValue(0);
                donate.setInsertOption();
                donateMap.put(type, donate);
            }
            return donate;
        }
    }

    public static Map<Long, Integer>  getMemberItemDonateList(int activityId, String unionUid, int type) {
        Map<Long, Integer> dataMap = new HashMap<>();
        Map<String, Map<Long, Map<Integer, StrongholdItemDonate>>> unionMap = itemDonateMap.get(activityId);
        if (unionMap == null) {
            return dataMap;
        }
        Map<Long, Map<Integer, StrongholdItemDonate>> userMap = unionMap.get(unionUid);
        if (userMap == null) {
            return dataMap;
        }
        for (Map<Integer, StrongholdItemDonate> donateMap : new ArrayList<>(userMap.values())) {
            for (StrongholdItemDonate donate : new ArrayList<>(donateMap.values())) {
                long userId = donate.getUserId();
                if (!dataMap.containsKey(userId)) {
                    dataMap.put(userId, 0);
                }
                dataMap.put(userId, dataMap.get(userId) + donate.getValue());
            }
        }

        return dataMap;
    }


    @Override
    public boolean reloadData() throws Exception {
        if (silverDonateMap == null) {
            silverDonateMap = new ConcurrentHashMap<>();
        }

        if (itemDonateMap == null) {
            itemDonateMap = new ConcurrentHashMap<>();
        }

        if (defendPatronsMap == null) {
            defendPatronsMap = new ConcurrentHashMap<>();
        }

        if (activityUnionBaseMap == null) {
            activityUnionBaseMap = new ConcurrentHashMap<>(2);
        }
        if (tradeMap == null) {
            tradeMap = new ConcurrentHashMap<>();
        }
        ActivityInfo activityInfo = StrongholdActivityMgr.getActivityInfo();
        uploadMarkMap = new ConcurrentHashMap<>();
        if (activityInfo != null) {
            int activityId = activityInfo.getActivityId();
            Map<String, Map<Long, StrongholdSilverDonate>> donateMap = StrongholdWarDonateBusiness.getSilverDonate(activityId);
            silverDonateMap.put(activityId, donateMap);
            Map<String, StrongholdUnionBase> unionBaseMap = StrongholdWarDonateBusiness.getUnionItemDonate(activityId);
            activityUnionBaseMap.put(activityId, unionBaseMap);

            Map<String, Map<Long, List<StrongholdEliminateDefendPatrons>>> unionPatronsMap = UserStrongholdBusiness.getEliminatePatrons(activityId);
            defendPatronsMap.put(activityId, unionPatronsMap);
            Map<Integer, StrongholdUploadMark> markMap = UserStrongholdBusiness.getUploadMarkMap(activityId);
            uploadMarkMap.put(activityId, markMap);
            Map<Long, StrongholdTradeData> dataMap = UserStrongholdBusiness.getStrongholdTradeMap(activityId);
            tradeMap.put(activityId, dataMap);
            itemDonateMap.put(activityId, StrongholdWarDonateBusiness.getStrongholdUserItemDonate(activityId));
        }


        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    private static boolean noticeTradeRed;

    @Override
    public boolean save() {
        for (Map<String, Map<Long, StrongholdSilverDonate>> activityMap : silverDonateMap.values()) {
            for (Map<Long, StrongholdSilverDonate> donateMap : new ArrayList<>(activityMap.values())) {
                for (StrongholdSilverDonate donate : new ArrayList<>(donateMap.values())) {
                    if (donate.isInsertOption()) {
                        StrongholdWarDonateBusiness.addSilverDonate(donate);
                    } else if (donate.isUpdateOption()) {
                        StrongholdWarDonateBusiness.updateSilverDonate(donate);
                    }
                }
            }
        }

        for (Map<String, StrongholdUnionBase> unionBaseMap : activityUnionBaseMap.values()) {
            for (StrongholdUnionBase unionBase : new ArrayList<>(unionBaseMap.values())) {
                if (unionBase.isInsertOption()) {
                    StrongholdWarDonateBusiness.addUnionItemDonate(unionBase);
                } else if (unionBase.isUpdateOption()) {
                    StrongholdWarDonateBusiness.updateUnionItemDonate(unionBase);
                }
            }
        }

        for (Map<String, Map<Long, StrongholdSilverDonate>> unionMap : silverDonateMap.values()) {
            for (Map<Long, StrongholdSilverDonate> donateMap : new ArrayList<>(unionMap.values())) {
                for (StrongholdSilverDonate silverDonate : new ArrayList<>(donateMap.values())) {
                    if (silverDonate.isInsertOption()) {
                        StrongholdWarDonateBusiness.addSilverDonate(silverDonate);
                    } else if (silverDonate.isUpdateOption()) {
                        StrongholdWarDonateBusiness.updateSilverDonate(silverDonate);
                    }
                }
            }
        }

        for (Map<String, Map<Long, List<StrongholdEliminateDefendPatrons>>> unionMap : defendPatronsMap.values()) {
            for (Map<Long, List<StrongholdEliminateDefendPatrons>> patronsMap : new ArrayList<>(unionMap.values())) {
                for (List<StrongholdEliminateDefendPatrons> patronsList : new ArrayList<>(patronsMap.values())) {
                    for (StrongholdEliminateDefendPatrons patrons : patronsList) {
                        if (patrons.isInsertOption()) {
                            UserStrongholdBusiness.addEliminatePatrons(patrons);
                        } else if (patrons.isUpdateOption()) {
                            UserStrongholdBusiness.updateEliminatePatrons(patrons);
                        }
                    }
                }
            }
        }

        for (Map<Integer, StrongholdUploadMark> markMap : uploadMarkMap.values()) {
            for (StrongholdUploadMark mark : markMap.values()) {
                if (mark.isInsertOption()) {
                    UserStrongholdBusiness.addUploadMark(mark);
                } else if (mark.isUpdateOption()) {
                    UserStrongholdBusiness.updateUploadMark(mark);
                }
            }
        }

        for (Map<Long, StrongholdTradeData> dataMap : tradeMap.values()) {
            for (StrongholdTradeData data : new ArrayList<>(dataMap.values())) {
                if (data.isInsertOption()) {
                    UserStrongholdBusiness.addStrongholdTrade(data);
                } else if (data.isUpdateOption()) {
                    UserStrongholdBusiness.updateStrongholdTrade(data);
                }
            }
        }

        for (Map<String, Map<Long, Map<Integer, StrongholdItemDonate>>> mapMap : itemDonateMap.values()) {
            for (Map<Long, Map<Integer, StrongholdItemDonate>> map : new ArrayList<>(mapMap.values())) {
                for (Map<Integer, StrongholdItemDonate> donateMap : new ArrayList<>(map.values())) {
                    for (StrongholdItemDonate donate : new ArrayList<>(donateMap.values())) {
                        if (donate.isInsertOption()) {
                            StrongholdWarDonateBusiness.addUserItemDonate(donate);
                        } else if (donate.isUpdateOption()) {
                            StrongholdWarDonateBusiness.updateUserItemDonate(donate);
                        }
                    }
                }
            }
        }

        return true;
    }

    private static void checkTradeReturn() {
        ActivityInfo activityInfo = StrongholdActivityMgr.getOpenActivityInfo();
        if (activityInfo == null) {
            return;
        }
        long returnTime = StrongholdActivityMgr.getTradeReturnTime();
        if (System.currentTimeMillis() > returnTime) {
            Map<Long, StrongholdTradeData> tradeDataMap = tradeMap.get(activityInfo.getActivityId());
            if(null != tradeDataMap) {
                for (StrongholdTradeData data : tradeDataMap.values()) {
                    if (data.isBack()) {
                        continue;
                    }
                    data.setBack(true);
                    long userId = data.getUserId();
                    String language = UserMgr.getLanguage(userId);
                    StrongholdTradeConfig tradeConfig = StrongholdActivityMgr.getTradeConfig(data.getAddressId());
                    String title = MultipleLanguageMgr.getContent(MailManager.STRONGHOLD_TRADE_TITLE, language);
                    String content = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.STRONGHOLD_TRADE_CONTENT, language),
                            MultipleLanguageMgr.getContent(tradeConfig.getName(), language));
                    MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), tradeConfig.getRewards(), content, title);
                }
            }
        }
    }

    public static long getOtherAddition(GamePlayer player){
        return CurioMgr.getSkillAdditionBySkillType(eSkillType.StrongholdWarPatronsSkill.getValue(), player, 0);
    }

    @Override
    public boolean stop() {
        return false;
    }
}
